<template>
	<view class="content">
		<view class="title">
			欢迎使用
		</view>
		<view class="desc">
			为了保证您能正常使用生产协同相关功能需要向您申请一下权限
		</view>

		<view class="choose" v-for="(item,index) in chooseList" :key="item.id" @click="choose(item,index)"
			:class="item.checked ? 'checked' : ''">
			<view class="img">
				<image v-if="item.checked" :src="item.img_1"></image>
				<image v-else :src="item.img"></image>
			</view>
			<view class="txt-icon">
				<view class="txt">
					<view class="txt-title">
						{{item.title}}
					</view>
					<view class="txt-desc">
						{{item.desc}}
					</view>
				</view>
				<view class="icon">
					<image v-if="item.checked" src="../../static/permission/checked.png" mode=""></image>
					<image v-else src="../../static/permission/uncheck.png" mode=""></image>
				</view>
			</view>
		</view>

		<view class="footer">
			<view class="tips">
				您也可以在系统中关闭授权，关闭后可能影响到部分功能的正常使用
			</view>
			<view class="read">
				<p>请您在使用生产协同前仔细阅读并同意：<span @click="userAgreement">《用户协议》</span>&<span
						@click="privacyPolicy">《隐私政策》</span></p>
			</view>
			<view class="btns">
				<u-button class="btn" type="warning" @click="no">不同意</u-button>
				<u-button class="btn" type="primary" @click="yes">同意</u-button>
			</view>
		</view>

	</view>
</template>

<script>
	// #ifdef APP-PLUS
	import permision from "@/common/permission.js"
	// #endif
	export default {
		data() {
			return {
				chooseList: [{
						id: 0,
						img: '../../static/permission/save.png',
						img_1: '../../static/permission/save_1.png',
						title: '存储',
						desc: '开启权限方便您存储文件',
						checked: true
					},
					{
						id: 1,
						img: '../../static/permission/local.png',
						img_1: '../../static/permission/local_1.png',
						title: '存储',
						desc: '开启权限方便您存储文件',
						checked: true
					},
					{
						id: 2,
						img: '../../static/permission/voice.png',
						img_1: '../../static/permission/voice_1.png',
						title: '存储',
						desc: '开启权限方便您存储文件',
						checked: true
					},
					{
						id: 3,
						img: '../../static/permission/camera.png',
						img_1: '../../static/permission/camera_1.png',
						title: '存储',
						desc: '开启权限方便您存储文件',
						checked: true
					},
				]
			}
		},
		methods: {
			choose(item, index) {
				this.chooseList[index].checked = !this.chooseList[index].checked
			},

			// 用户协议
			userAgreement() {},

			// 隐私政策
			privacyPolicy() {},

			// 不同意
			no() {
				var platform = uni.getSystemInfoSync().platform;
				platform === 'android' ?
					plus.runtime.quit() :
					plus.ios.import("UIApplication").sharedApplication().performSelector("exit")
			},

			// 同意
			yes() {
				var platform = uni.getSystemInfoSync().platform;
				console.log('platform', platform);
				const checkList = this.chooseList.map(item => {
					return item.checked
				})
				if (platform == 'android') {
					const permissionList = [
						['android.permission.READ_EXTERNAL_STORAGE',
							'android.permission.WRITE_EXTERNAL_STORAGE'
						], //存储读写
						['android.permission.ACCESS_FINE_LOCATION'], // 位置
						['android.permission.RECORD_AUDIO'], //录音
						['android.permission.CAMERA'] //摄像
					]
					for (let i in checkList) {
						checkList[i] && this.androidRequestPermissions(permissionList[i])
					}
				} else {
					for (let i in checkList) {
						checkList[i] && this.iosRequestPermissions(i)
					}
				}
			},

			judgeIosPermission: function(permisionID) {
				var result = permision.judgeIosPermission(permisionID)
				console.log(result);
				var strStatus = (result) ? "已" : "未"
				uni.showModal({
					content: permisionID + '权限' + strStatus + "获得授权",
					showCancel: false
				});
			},

			androidRequestPermissions(permisionID) {
				plus.android.requestPermissions(permisionID, function(e) {
					if (e.deniedAlways.length > 0) { //权限被永久拒绝  
						console.log('权限被永久拒绝' + e.deniedAlways.toString());
					}
					if (e.deniedPresent.length > 0) { //权限被临时拒绝  
						console.log('权限被临时拒绝' + e.deniedPresent.toString());
					}
					if (e.granted.length > 0) { //权限被允许  
						console.log('权限被允许' + e.granted.toString());
					}
				}, function(e) {
					console.log('Request Permissions error:' + JSON.stringify(e));
				});
			},

			iosRequestPermissions(index) {
				if (index === '0') {
					this.iosPermission('album', '照片');
				} else if (index === '1') {
					uni.getLocation({
						success: res => console.log(res)
					});
				} else if (index === '2') {
					const recorderManager = uni.getRecorderManager();
					recorderManager.start();
					recorderManager.stop();
				} else {
					this.iosPermission('camera', '相机');
				}
			},

			iosPermission(sys, name) {
				console.log(sys, name);
				let status = permision.requestIOS(sys)
				if (status === null || status === 1) {
					status = 1;
				} else if (status === 2) {
					console.log(`系统${name}权限已关闭`);
				} else {
					uni.showModal({
						content: `需要${name}权限`,
						confirmText: "设置",
						success: function(res) {
							if (res.confirm) {
								permision.gotoAppSetting();
							}
						}
					})
				}
			}
		},
	}
</script>

<style lang="scss" scoped>
	.content {
		padding: 100rpx 20rpx 20rpx;

		.title {
			font-size: 50rpx;
			font-weight: 700;
		}

		.desc {
			font-size: 28rpx;
			color: #1e1f35;
			margin-top: 20rpx;
			padding-bottom: 20rpx;
		}

		.choose {
			width: 95%;
			margin: 30rpx auto;
			border: 2px solid #BBBCBE;
			display: flex;
			align-items: center;
			padding: 20rpx 0 20rpx 20rpx;
			border-radius: 8rpx;

			.img {
				width: 70rpx;
				height: 70rpx;

				image {
					width: 100%;
					height: 100%;
				}
			}

			.txt-icon {
				display: flex;
				align-items: center;
				width: 85%;
				justify-content: space-between;

				.txt {
					margin-left: 20rpx;
					color: #BBBCBE;
					
					.txt-title {
						font-size: 40rpx;
						font-size: 700;
					}

					.txt-desc {
						font-size: 24rpx;
					}
				}

				.icon {
					width: 40rpx;
					height: 40rpx;
					cursor: pointer;

					image {
						width: 100%;
						height: 100%;
					}
				}
			}
		}

		.choose.checked {
			border: 2px solid #1296db;
			
			.txt {
				color: #1296db;
			}
		}

		.footer {
			position: fixed;
			width: 95%;
			margin: 0 auto;
			bottom: 40rpx;
			padding-top: 30rpx;
			border-top: 1px solid #E6E6E6;

			.tips {
				font-size: 24rpx;
				color: #BBBCC6;
				margin-top: 30rpx;
			}

			.read {
				font-size: 24rpx;
				color: #BBBCC6;
				margin-top: 30rpx;

				span {
					color: #0043fa;
				}
			}

			.btns {
				display: flex;
				justify-content: space-around;
				padding-top: 50rpx;

				.btn {
					flex: 1;
					margin: 0 20rpx;
				}
			}
		}
	}
</style>
